package com.example.demo.service.impl;

import com.example.demo.Util.Result;
import com.example.demo.Util.ResultUtil;
import com.example.demo.mapper.GradeMap;
import com.example.demo.po.Lab;
import com.example.demo.po.grade;
import com.example.demo.service.GradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import java.util.List;

@Service
public class GradeServiceImpl implements GradeService {

    @Autowired
    GradeMap gradeMap;


    @Override
    public Result SaveGrade(grade g) {
        if(g == null){
            return ResultUtil.fail("NUll Exception");
        }
        if(StringUtils.isEmpty(g.getName())){
            return ResultUtil.fail("名称必填！");
        }

        //重名
        List<grade> list = gradeMap.GetGradeListByName(g.getName());
        if( list.size()>0&&list.get(0).getId()!=g.getId()) {
            return ResultUtil.fail("名称重复了");
        }

        gradeMap.SaveGrade(g);

       return ResultUtil.success(g);
    }

    @Override
    public void DeleteGrade(int id) {

    }

    @Override
    public Result UpdateGrade(grade g) {

        if(g==null){
            return ResultUtil.fail("空对象异常了");
        }

        if(StringUtils.isEmpty(g.getName())){
            return ResultUtil.fail("名称必填！");
        }

        //重名
        List<grade> list = gradeMap.GetGradeListByName(g.getName());
        if( list.size()>0&&list.get(0).getId()!=g.getId()) {
            return ResultUtil.fail("名称重复了");
        }


        gradeMap.UpdateGrade(g);
        return ResultUtil.success("修改成功");
    }
}
